*** Table 1 ***	
	
use "$data/simulated_df.dta", clear 

********************************************************************************	
*** Rename some variables for brevity ***
********************************************************************************

cap drop pre_vc
ren (pre_any_vc any_pre_total_fpds_over50k ///
	any_pre_total_patent any_pre_total_allsbir_diff ///
	any_pre_total_hi_cite any_pre_total_hi_orig any_pre_total_app) ///
	(pre_vc pre_fpds pre_patent pre_sbir pre_cite pre_orig pre_app)

cap drop post_vc
ren (post_any_vc1095 any_post_total_fpds_over50k1095 ///
	any_post_total_patent1095 any_post_total_hi_cite1095 ///
	any_post_total_hi_orig1095 any_post_total_allsbir_diff) ///
	(post_vc post_fpds post_patent post_cite post_orig post_sbir)

ren tvar_ac_distance cossim 
ren employees_at_app employees

* Labels
lab var num_per_topic "Num Proposals per Topic"
lab var winners_per_topic "Num Winners per Topic"
lab var age "Age"
lab var employees "Number of Employees"
lab var in_vchub "$\mathbbm{1}$(in VC Hub)"
lab var in_afbase_city "$\mathbbm{1}$(in County with AF Base)"
lab var minority "$\mathbbm{1}$(Minority Owned)"
lab var woman_own "$\mathbbm{1}$(Woman owned)"
lab var pre_vc "$\mathbbm{1}$(Previous VC)"
lab var pre_fpds "$\mathbbm{1}$(Previous DoD Contract)"
lab var pre_patent "$\mathbbm{1}$(Previous Patent)"
lab var pre_sbir "$\mathbbm{1}$(Previous SBIR Contract)"
lab var pre_orig "$\mathbbm{1}$(Previous High Originality Patent)"
lab var post_vc "$\mathbbm{1}$(VC)"
lab var post_fpds "$\mathbbm{1}$(DoD Contract)"
lab var post_patent "$\mathbbm{1}$(Patent)"
lab var post_sbir "$\mathbbm{1}$($\geq 2$ SBIR)"
lab var post_orig "$\mathbbm{1}$(High Originality Patent)"
lab var pre_cite "$\mathbbm{1}$(Previous High Citation Patent)"
lab var pre_app "$\mathbbm{1}$(Previous Patent Application)"
lab var post_cite "$\mathbbm{1}$(High Citation Patent)"
lab var software "$\mathbbm{1}$(Software)"
lab var foreign_national_flag "$\mathbbm{1}$(Immigrant)"
lab var rank_v2 "Normalized Rank"
lab var cossim "Non-specificity Index"

********************************************************************************
********** Summarize high/low specificity and open/conv applicants **********
********************************************************************************

foreach var of varlist in_vchub in_afbase_city age ///
	employees foreign_national_flag minority woman_own ///
	pre_fpds pre_vc pre_patent pre_orig pre_cite pre_sbir ///
	num_per_topic frac_win winners_per_topic cossim { 
		
		local l`var': variable label `var'
		
		cap drop one_per_topic
		bys topic_n phase firm_first_open_proposal firm_first_conv_proposal ///
			pre_total_allsbir: gen one_per_topic=_n
		if inlist("`var'", "num_per_topic", "winners_per_topic", "cossim") {
			local oneper "& one_per_topic==1"
			local clus "topic_n"
			bys topic_n phase: egen `var'm = mean(`var')
			ren `var' `var'_orig 
			ren `var'm `var'
		}
		else {
			if inlist("`var'", "awardamount", "total_score_orig_win", ///
				      "team_score_orig_win", "comm_score_orig_win", ///
					  "tech_score_orig_win", "frac_win") {
				local oneper "& one_per_topic==1"
				local clus "topic_n"
				bys topic_n phase: egen `var'm = mean(`var')
				ren `var' `var'_orig 
				ren `var'm `var'
			}
			else {
				local oneper ""
				local clus "firm_id_n"
			}
		}
				
		* By open/conv 
		foreach x in 0 1 {
			sum `var' if open==`x' & ///
				(firm_first_open_proposal==1 | firm_first_conv_proposal==1) & ///
				pre_total_allsbir==0 & year > 2016 & phase==1  `oneper', det
			local `var'_o`x'n: di %10.0fc `r(N)'
			local `var'_o`x'mu: di %10.3fc `r(mean)'
			local `var'_o`x'sd: di %10.3fc `r(sd)'
			local `var'_o`x'p50: di %10.0fc `r(p50)'
			local `var'_o`x'min: di %10.3fc `r(min)'
			local `var'_o`x'max: di %10.3fc `r(max)'
		}
		
		qui ttest `var' if ///
			( firm_first_open_proposal==1 | firm_first_conv_proposal==1 ) & ///
			pre_total_allsbir==0 & year > 2016 & phase==1 `oneper', by(open)
		local `var'_od = `r(mu_2)' - `r(mu_1)' 
		local `var'_od: di %10.3f ``var'_od'
		qui reg `var' i.open if ( firm_first_open_proposal==1 | ///
			firm_first_conv_proposal==1 ) & pre_total_allsbir==0 & year > 2016 & phase==1 `oneper', ///
			cluster(`clus')
		local t = _b[1.open]/_se[1.open]
		local p =2*ttail(e(df_r),abs(`t'))
		
		if `p' <= 0.01 {
			local `var'_od "``var'_od'$^{***}$"
		}
		if `p' > 0.01 & `p' <= 0.05 {
			local `var'_od "``var'_od'$^{**}$"
		}
		if `p' > 0.05 & `p' <= 0.1 {
			local `var'_od "``var'_od'$^*$"
		}
		
		local `var'_op : di %10.3f `p'
		
		if inlist("`var'", "num_per_topic", "winners_per_topic", "frac_win", "cossim") | inlist("`var'", "awardamount", "total_score_orig_win", ///
				      "team_score_orig_win", "comm_score_orig_win", ///
					  "tech_score_orig_win") {
					  	drop `var'
						ren `var'_orig `var'
					  }
		
		cap drop one_per_topic
		bys topic_n phase: gen one_per_topic = _n 
		
		bys topic_n phase: egen `var'm = mean(`var')
		ren `var' `var'_orig 
		ren `var'm `var'
		
		local oneper "& one_per_topic==1 & winners_per_topic!=0" 
		local clus "topic_n"

	}


* Open and conventional 
cap file close des
file open des using "$tables/table1.tex", write replace
file write des
file write des "\begin{footnotesize}" _n
file write des "\begin{tabular}{lcccccccc}" _n
file write des " \toprule" _n
file write des "& \multicolumn{3}{c}{Conventional} & \multicolumn{3}{c}{Open} \tabularnewline" _n
file write des "\cmidrule(lr){2-4} \cmidrule(lr){5-7} " _n
file write des " & \multirow{2}{*}{N} & \multirow{2}{*}{Mean} & \multirow{2}{*}{SD} & \multirow{2}{*}{N} & \multirow{2}{*}{Mean} & \multirow{2}{*}{SD} & Diff of \tabularnewline" _n
file write des " & &  & & & & & Means \tabularnewline" _n
file write des " & &  & & & & & (Open - Conv) \tabularnewline" _n
file write des "\midrule" _n

* Fill in locals
file write des "\multicolumn{8}{l}{\bf Company Characteristics} \tabularnewline" _n
//dropped age employees
file write des "\multicolumn{8}{l}{\it Lifecycle controls} \tabularnewline" _n
foreach var of varlist age employees {
	    file write des "`l`var'' & ``var'_o0n' & ``var'_o0mu' &  ``var'_o0sd' & ``var'_o1n' & ``var'_o1mu' & ``var'_o1sd' & ``var'_od'  \tabularnewline" _n
}

file write des " \tabularnewline" _n
file write des "\multicolumn{8}{l}{\it Additional controls} \tabularnewline" _n
foreach var of varlist in_vchub in_afbase_city ///
	foreign_national_flag minority woman_own {
	    file write des "`l`var'' & ``var'_o0n' & ``var'_o0mu' &  ``var'_o0sd' & ``var'_o1n' & ``var'_o1mu' & ``var'_o1sd' & ``var'_od'  \tabularnewline" _n
	
}

file write des " \tabularnewline" _n
file write des "\multicolumn{8}{l}{\bf Pre-award Outcomes} \tabularnewline" _n

foreach var of varlist pre_fpds pre_vc pre_patent pre_orig pre_cite pre_sbir {
		file write des "`l`var'' & ``var'_o0n' & ``var'_o0mu' &``var'_o0sd' & ``var'_o1n' & ``var'_o1mu' &``var'_o1sd' &``var'_od' \tabularnewline" _n
	}
file write des "  \tabularnewline" _n
file write des "\multicolumn{8}{l}{\bf Competition Characteristics} \tabularnewline" _n

foreach var of varlist  num_per_topic winners_per_topic cossim   {
		file write des "`l`var'' & ``var'_o0n' & ``var'_o0mu' & ``var'_o0sd' & ``var'_o1n' & ``var'_o1mu' & ``var'_o1sd' & ``var'_od'  \tabularnewline" _n
	
}

* Table footer
{
file write des " \bottomrule" _n
file write des " \tabularnewline" _n
file write des "\end{tabular}" _n
file write des "\end{footnotesize}" _n
}
file close des
di "Done!"
